CROSS_COMPILE 	?= arm-linux-gnueabihf-
TARGET		  	?= template

CC 				:= $(CROSS_COMPILE)gcc
LD				:= $(CROSS_COMPILE)ld
OBJCOPY 		:= $(CROSS_COMPILE)objcopy
OBJDUMP 		:= $(CROSS_COMPILE)objdump

LIBPATH			:= -lgcc -L /usr/local/arm/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf/lib/gcc/arm-linux-gnueabihf/4.9.4

INCDIRS 		:= imx6ul \
				   imx6ul_basic_func\
				   imx6ul_basic_func/sys_int\
				   imx6ul_basic_func/sys_clk\
				   imx6ul_basic_func/sys_tick\
				   imx6ul_basic_func/sys_debug\
				   imx6ul_basic_func/sys_thirdparty/stdio/include\
				   eason \
				   eason/hal/gpio\
				   eason/hdl/gpio\
				   eason/hal/led\
				   eason/hdl/led\
				   eason/hal/lcd\
				   eason/hdl/lcd\
				   eason/hal/epit_timer\
				   eason/hdl/epit_timer\
				   eason/hal/i2c\
				   eason/hdl/i2c\
				   eason/hal/ap3216c\
				   eason/hdl/ap3216c\
				   eason/hal/oled\
				   eason/hdl/oled\
				   eason/main_framework\
				   project \

SRCDIRS			:= imx6ul_basic_func/sys_int\
				   imx6ul_basic_func/sys_clk\
				   imx6ul_basic_func/sys_tick\
				   imx6ul_basic_func/sys_debug\
				   imx6ul_basic_func/sys_thirdparty/stdio/lib\
				   eason \
				   eason/hal/gpio\
				   eason/hdl/gpio\
				   eason/hal/led\
				   eason/hdl/led\
				   eason/hal/lcd\
				   eason/hdl/lcd\
				   eason/hal/epit_timer\
				   eason/hdl/epit_timer\
				   eason/hal/i2c\
				   eason/hdl/i2c\
				   eason/hal/ap3216c\
				   eason/hdl/ap3216c\
				   eason/hal/oled\
				   eason/hdl/oled\
				   eason/main_framework\
				   project \


INCLUDE			:= $(patsubst %, -I %, $(INCDIRS))

SFILES			:= $(foreach dir, $(SRCDIRS), $(wildcard $(dir)/*.S))
CFILES			:= $(foreach dir, $(SRCDIRS), $(wildcard $(dir)/*.c))

SFILENDIR		:= $(notdir  $(SFILES))
CFILENDIR		:= $(notdir  $(CFILES))

SOBJS			:= $(patsubst %, obj/%, $(SFILENDIR:.S=.o))
COBJS			:= $(patsubst %, obj/%, $(CFILENDIR:.c=.o))
OBJS			:= $(SOBJS) $(COBJS)

VPATH			:= $(SRCDIRS)

.PHONY: clean

$(TARGET).bin : $(OBJS)
	$(LD) -Timx6ul.lds -o $(TARGET).elf $^ $(LIBPATH)
	$(OBJCOPY) -O binary -S $(TARGET).elf $@
	$(OBJDUMP) -D -m arm $(TARGET).elf > $(TARGET).dis

$(SOBJS) : obj/%.o : %.S
	$(CC) -Wall -nostdlib -fno-builtin -c -O2  $(INCLUDE) -o $@ $<

$(COBJS) : obj/%.o : %.c
	$(CC) -Wall -Wa,-mimplicit-it=thumb -nostdlib -fno-builtin -c -O2  $(INCLUDE) -o $@ $<

lib:
	./make_lib.sh lib

lib_clean:
	./make_lib.sh 

clean:
	rm -rf $(TARGET).elf $(TARGET).dis $(TARGET).bin $(COBJS) $(SOBJS)

#/////////////////////////////////////////////////////////////////////////////////#
all:
	make lib && make

clean_all:
	make clean && make lib_clean

pack:
	chmod 777 imxdownload
	ls /dev/sd*
	./imxdownload $(TARGET).bin /dev/sdb
